package com.syq.controller;


import com.syq.service.*;
import com.syq.util.ResponseUtil;
import com.syq.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName: AdminController
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/22 11:01
 */

@CrossOrigin
@RestController
@RequestMapping("/admin/dashboard")
public class AdminController {

    @Autowired
    private DtsGoodsService dtsGoodsService;

    @Autowired
    private DtsCategoryService dtsCategoryService;

    @Autowired
    private DtsOrderService dtsOrderService;

    @Autowired
    private DtsGoodsProductService dtsGoodsProductService;

    @Autowired
    private DtsUserService dtsUserService;

    @GetMapping
    public Object dashboard(){
        Map<String,Object> map=new HashMap<>();

        //1商品数量
        Long goodsTotal=dtsGoodsService.findCount();
        //2用户
        Long userTotal=dtsUserService.findCount();
        //3货品
        Long productTotal=dtsGoodsProductService.findCount();
        //4订单
        Long orderTotal=dtsOrderService.findCount();


        map.put("goodsTotal",goodsTotal);
        map.put("userTotal",userTotal);
        map.put("productTotal",productTotal);
        map.put("orderTotal",orderTotal);
        return ResponseUtil.ok(map);
    }

    //首页图表
    @GetMapping("/chart")
    public Object chart(){
        Map<String,Object> map=new HashMap<>();

        //饼图
        CategorySellVo categorySellVo=new CategorySellVo();
        List<CategorySellAmts> categorySellVoList = dtsCategoryService.findCategorySellAmts();
        String[] collect = categorySellVoList.stream()
                .map(CategorySellAmts::getName)
                .collect((Collectors.toList()))
                .toArray(new String[categorySellVoList.size()]);
        categorySellVo.setCategorySellData(categorySellVoList);
        categorySellVo.setCategoryNames(collect);

        //柱表
        List<DayStatis> userOrderCntVoList=dtsUserService.findUserOrderCntVo();

        List<DayStatis> dayStatisList=dtsOrderService.findDayStatis();

        //日期对应的用户数和订单数
        UserOrderCntVo userOrderCntVo=new UserOrderCntVo();

        //获取日期
        String[] dayData = mergeDay(dayStatisList, userOrderCntVoList);

        userOrderCntVo.setDayData(dayData);
        //每日订单数及新增用户数
        userOrderCntVo.setOrderCnt(getCnts(dayData,userOrderCntVoList));
        userOrderCntVo.setUserCnt(getCnts(dayData,dayStatisList));
        //日期和对应的订单总额, 订单笔数
        OrderAmtsVo orderAmtsVo=new OrderAmtsVo();

        orderAmtsVo.setDayData(dayStatisList.stream()
                .map(DayStatis::getDayStr)
                .collect(Collectors.toList())
                .toArray(new String[dayStatisList.size()]));

        orderAmtsVo.setOrderCntData(dayStatisList.stream()
                .map(DayStatis::getCnts)
                .collect(Collectors.toList())
                .toArray(new Integer[dayStatisList.size()]));

        orderAmtsVo.setOrderAmtData(dayStatisList.stream()
                .map(DayStatis::getAmts)
                .collect(Collectors.toList())
                .toArray(new BigDecimal[dayStatisList.size()])
        );

        map.put("categorySell",categorySellVo);
        map.put("orderAmts",orderAmtsVo);
        map.put("userOrderCnt",userOrderCntVo);
        return ResponseUtil.ok(map);
    }

    public String[] mergeDay(List<DayStatis> dayStatisList,List<DayStatis> userOrderCntVoList){
        Set<String> day=new HashSet<>();
        dayStatisList.forEach(dayStatis -> {
            day.add(dayStatis.getDayStr());
        });
        userOrderCntVoList.forEach(dayStatis -> {
            day.add(dayStatis.getDayStr());
        });
        List<String> dayData=new ArrayList<>(day);
        Collections.sort(dayData);
        return dayData.toArray(new String[dayData.size()]);
    }

    public Integer[] getCnts(String[] dayData,List<DayStatis> dayStatisList){
        Integer[] cnt=new Integer[dayData.length];
        for (int i = 0; i < cnt.length; i++) {
            int dayCnt=0;
            String dayStr=dayData[i];
            for (DayStatis dayStatis : dayStatisList) {
                if (dayStr.equals(dayStatis.getDayStr())){
                    dayCnt=dayStatis.getCnts();
                    break;
                }
            }
            cnt[i]=dayCnt;
        }
        return cnt;
    }
}
